<template>
    <div class="login-page">
      <el-card>
        <template #header>黑马面经运营后台</template>
        <el-form :model="form" :rules="rules" ref="loginForm" autocomplete="off">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="form.username" placeholder="输入用户名"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="form.password" type="password" placeholder="输入用户密码"></el-input>
          </el-form-item>
          <el-form-item class="tc">
            <el-button type="primary" @click="onLogin">登 录</el-button>
            <el-button @click="onReset">重 置</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </template>

<script>

export default {
  name: 'login-page',
  data () {
    return {
      // 数据
      form: {
        username: '',
        password: ''
      },
      // 校验规则对象
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: ['blur', 'change'] },
          { min: 5, max: 11, message: '长度在 5 到 11 个字符', trigger: ['blur', 'change'] }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: ['blur', 'change'] },
          { pattern: /^\w{5,10}$/, message: '长度在 5 到 10 个字符', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  methods: {
    async onLogin () {
      // 对表单进行整体校验
      try {
        await this.$refs.loginForm.validate()
        this.$store.dispatch('user/login', this.form)
      } catch (error) {
        console.log(error)
      }
    },
    onReset () {
      this.$refs.loginForm.resetFields()
    }
  }
}
</script>

  <style lang="scss" scoped>
  .login-page {
    min-height: 100vh;
    background: url(@/assets/login-bg.svg) no-repeat center / cover;
    display: flex;
    align-items: center;
    justify-content: space-around;

    .el-card {
      width: 420px;

      ::v-deep .el-card__header {
        height: 80px;
        background: rgba(114, 124, 245, 1);
        text-align: center;
        line-height: 40px;
        color: #fff;
        font-size: 18px;
      }
    }

    .el-form {
      padding: 0 20px;
    }

    .tc {
      text-align: center;
    }
  }
  </style>
